import sys
from PyQt6 import uic
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QApplication, QLineEdit, QComboBox, QGroupBox, QSizePolicy,QTabWidget
from PyQt6.QtWidgets import QWidget,QVBoxLayout,QLabel

class RightWidget(QWidget):
    def __init__(self,parent = None):
        super().__init__(parent)
        self.init_ui()
    def init_ui(self):
        layout = QVBoxLayout(self)

        try:
            uic.loadUi("干扰源配置.ui", self)
            self.apply_styles()
            self.set_background()
            self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        except FileNotFoundError:
            print("ui文件未找到，使用备用布局")
            self.label = QLabel("干扰源配置", self)
            self.label.setStyleSheet("font-size:24px;text-align:center;")
            layout.addWidget(self.label)
            return

    def set_background(self):
        self.setStyleSheet("background-color:white;")

    def apply_styles(self):
        # 输入框：圆角和边框修改
        line_edits = [
            self.findChild(QLineEdit, "lineEdit"),
            self.findChild(QLineEdit, "lineEdit_2"),
            self.findChild(QLineEdit, "lineEdit_3"),
            self.findChild(QLineEdit, "lineEdit_4")
        ]
        for line_edit in line_edits:
            if line_edit:
                line_edit.setStyleSheet("border-radius: 3px;border:1px solid #d3d3d3")

        # 修改GroupBox边框颜色
        groupbox = self.findChild(QGroupBox,"groupBox")
        if groupbox:
            groupbox.setStyleSheet("""QGroupBox {
                                         border:0.5px solid #999999;
                                         border-radius:2px
                                         }""")

        # 下拉框：红色箭头和箭头图标修改，添加下拉框内容
        all_combos = self.findChildren(QComboBox)
        for combo_edit in all_combos:
            combo_edit: QComboBox
            arrow_style = """
                        QComboBox::drop-down {
                            background-color: #cc0000;
                        }
                        QComboBox::down-arrow {
                            width:8px;
                            height:8px;
                            image:url(arrow.png);       
                         }
                         QComboBox {
                         color : #000000;
                         border:1px solid #d3d3d3;
                         }
                        """
            base_style = arrow_style
            if combo_edit.objectName() in ["comboBox", "comboBox_3"]:
                base_style += """
                                   QComboBox {
                                   color: #808080;
                                   }"""
            combo_edit.setStyleSheet(base_style)

            if combo_edit.objectName() == "comboBox":
                combo_edit.addItem("TX2")

            elif combo_edit.objectName() == "comboBox_2":
                combo_edit.addItem("30720000")

            elif combo_edit.objectName() == "comboBox_3":
                combo_edit.addItem("TX2")


        # QTabWidget标签字体修改
        tabWidget = self.findChild(QTabWidget,"tabWidget")
        tabWidget2 = self.findChild(QTabWidget,"tabWidget_2")
        font = QFont()
        font.setPointSize(7)
        if tabWidget:
           tabWidget.setFont(font)
        if tabWidget2:
           tabWidget2.setFont(font)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    right = RightWidget()
    right.show()
    sys.exit(app.exec())